package com.gxa.park.manager.advice;

import com.gxa.park.core.commom.JsonResult;
import com.gxa.park.core.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindException;
import org.springframework.validation.Errors;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import java.util.Map;
import java.util.Set;

/**
 * @date 2022/6/29
 */
@RestController
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    // 处理第一种校验异常: 将异常信息拼接,返回给前端显示
    @ExceptionHandler({BindException.class,
            MethodArgumentNotValidException.class})
    public JsonResult handleBindException(Errors errors){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        StringBuilder stringBuilder = new StringBuilder();
        errors.getAllErrors().forEach(error->{
            stringBuilder.append(error.getDefaultMessage()+",");
        });
        jsonResult.setMsg(stringBuilder.toString());
        return jsonResult;
        }
    // 处理第二种校验异常: 将异常信息拼接,返回给前端显示
    @ExceptionHandler(ConstraintViolationException.class)
    public JsonResult
    handleConstraintViolationException(ConstraintViolationException exception) {
        StringBuilder errorInfo = new StringBuilder();
        String errorMessage ;
        Set<ConstraintViolation<?>> violations =
                exception.getConstraintViolations();
        for (ConstraintViolation<?> item : violations) {
            errorInfo.append(item.getMessage()).append(",");
        }
        errorMessage = errorInfo.toString().substring(0,
                errorInfo.toString().length()-1);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg(errorMessage);
        return jsonResult;
    }
    // 处理业务异常: 将异常信息返回给前端显示
    @ExceptionHandler(BusinessException.class)
    public JsonResult
    handleBusinessException(BusinessException exception) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg(exception.getMessage());
        return jsonResult;
    }
// 处理系统异常: 将异常信息返回给前端打哈哈,系统后台记录日志
    @ExceptionHandler(Exception.class)
    public Map handleException(Exception e) {
//        1.给前端返回异常状态码和统一话术
//        2.记录日志(log4j2/slf4j/nohup.out)

// 把系统异常日志记录
        log.info("-----------------begin-----------------------"); //分割线方便阅读
        // 先记录异常对象的toString状态: 包括异常类型和异常原因
            log.info(e.getMessage());
// 再遍历栈中的异常轨迹: 采用遍历方式
        StackTraceElement[] stackTrace = e.getStackTrace();
        for (StackTraceElement stackTraceElement :
                stackTrace) {
            log.info(stackTraceElement.toString());
        }
        log.info("-------------------end---------------------");//分割线方便阅读
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("0");
        jsonResult.setMsg("系统开小差了,请稍后再试");
        return jsonResult.getMap();
    }


    }

